package com.example.common.utils;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author DestinyCc
 * @date 2022/4/19
 * python脚本执行工具，无返回值，用于清空python脚本输出流，防止出现死锁
 * 使用方法：Process process = Runtime.getRuntime().exec(cmd);
 *           new PythonScriptExecutor(process.getInputStream()).start();
 *           process.waitFor();
 */
@Slf4j
public class PythonScriptExecutor extends Thread{
    private InputStream is;

    public PythonScriptExecutor(InputStream is){
        this.is = is;
    }

    @Override
    @SneakyThrows
    public void run(){
        BufferedReader in = new BufferedReader(new InputStreamReader(this.is));
        String line;
        while ((line = in.readLine()) != null) {
            log.info("Python output: {}", line);
            //do nothing
        }
    }
}
